package com.zyh.server.utils;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;
/**
 * @Author: zyh
 * @Date: 2024-11-13-10:40
 * @Description 日期格式化工具
 */
public class CustomDateTimeSerializer  extends JsonSerializer<Date>{
	private SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	@Override
	public void serialize(Date date, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
		//设置时区
		sdf.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
		//将Date--->String
		String timeStr = sdf.format(date);
		//将timeStr写入到磁盘中
		jsonGenerator.writeString(timeStr);
	}
}
